﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Dynamic;
using SDT.Admin.Properties;
using SDT.Admin.ViewModels;
using SDT.Model;
using SDT.Model.Properties;
using SDT.Lib.Common;
using SDT.Lib.Paging;
using SDT.Lib.Helpers;
namespace SDT.Admin.Controllers
{
    [RoleHelper(Roles = RoleSystem.Dictionary)]
    public class CompanyTypeController : Controller
    {
        /// <summary>
        /// Created by Vietlv
        /// Date 25/07/2011
        /// Description: Manage CompanyType - Class CompanyType controler;
        /// </summary>
        /// <returns></returns>

        SDTDataContext eDataConttext = new SDTDataContext();

        #region GET: /CompanyType/
        public ActionResult Index(int? page)
        {
            try
            {
                if (UserManager.CheckRole(RoleSystem.Dictionary))
                {
                    if (page == null) page = 1;
                    string order = UserState.getUserStateFromRequest("ProductType.orderColum", "order", "ID").ToString();
                    string orderDir = UserState.getUserStateFromRequest("ProductType.orderDirection", "orderDir", "desc").ToString();
                    string keyword = UserState.getUserStateFromRequest("ProductType.Keyword", "Keyword", "").ToString();
                    int pagesize = Convert.ToInt32(UserState.getUserStateFromRequest("ProductType.PageSize", "PageSize", Settings.Default.PageSize));
                    var listCompanyType = eDataConttext.CompanyTypes
                        .Where(e => keyword.Trim().Length > 0 ? e.Name.ToLower().Contains(keyword.ToLower().Trim()) ||
                                e.Description.ToLower().Contains(keyword.ToLower().Trim()) : true)
                        .OrderBy(string.Format("{0} {1}", order, orderDir))
                        .ToPagedList(page.Value - 1, pagesize);
                    ViewData["order"] = order;
                    ViewData["orderDir"] = orderDir;
                    return View(new CompanyTypeViewModel
                    {
                        CompanyTypes = listCompanyType,
                        Keyword = keyword
                    });
                }
                else
                {
                    ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
        #endregion

        #region GET: /CompanyType/Details/5

        public ActionResult Details(Byte id)
        {
            if (UserManager.CheckRole(RoleSystem.Dictionary))
            {
                var itemCompanyType = eDataConttext.CompanyTypes.Single(e => e.ID == id);
                return View(itemCompanyType);
            }
            else
            {
                ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                return View();
            }
        }
        #endregion

        #region GET: /CompanyType/Create

        public ActionResult Create()
        {
            if (UserManager.CheckRole(RoleSystem.Dictionary))
            {
                return View();
            }
            else
            {
                ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                return View();
            }

        }
        #endregion

        #region POST: /CompanyType/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(CompanyType fromdata)
        {
            if (UserManager.CheckRole(RoleSystem.Dictionary))
            {
                try
                {
                    UpdateModel(fromdata);
                    fromdata.AddDate = DateTime.Now;
                    eDataConttext.CompanyTypes.InsertOnSubmit(fromdata);
                    eDataConttext.SubmitChanges();

                    return RedirectToAction("Index");
                }
                catch (Exception exp)
                {
                    ViewData[Messages.MessageInfo] = exp.Message;
                    return View();
                }
            }
            else
            {
                ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                return View();
            }
        }
        #endregion

        #region GET: /CompanyType/Delete/5

        public ActionResult Delete(int id)
        {
            if (UserManager.CheckRole(RoleSystem.Dictionary))
            {
                try
                {
                    var item = eDataConttext.CompanyTypes.Single(e => e.ID == id);

                    return View(item);
                }
                catch (Exception exp)
                {
                    ViewData[Messages.MessageInfo] = exp.Message;
                    return View();
                }
            }
            else
            {
                ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                return View();
            }
        }
        #endregion

        #region POST: /CompanyType/Delete/5

        [HttpPost]
        public ActionResult Delete(Byte id, FormCollection collection)
        {
            if (UserManager.CheckRole(RoleSystem.Dictionary))
            {
                try
                {
                    var item = eDataConttext.CompanyTypes.Single(e => e.ID == id);
                    eDataConttext.CompanyTypes.DeleteOnSubmit(item);
                    eDataConttext.SubmitChanges();
                    return RedirectToAction("Index");
                }
                catch (Exception exp)
                {
                    ViewData[Messages.MessageInfo] = exp.Message;
                    return View();
                }
            }
            else
            {
                ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                return View();
            }
        }
        #endregion

        #region GET: /CompanyType/Edit/5

        public ActionResult Edit(int id)
        {
            if (UserManager.CheckRole(RoleSystem.Dictionary))
            {
                try
                {
                    var item = eDataConttext.CompanyTypes.Single(e => e.ID == id);

                    return View(item);
                }
                catch (Exception exp)
                {
                    ViewData[Messages.MessageInfo] = exp.Message;
                    return View();
                }
            }
            else
            {
                ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                return View();
            }
        }
        #endregion

        #region POST: /CompanyType/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(int id, CompanyType fromdata)
        {
            if (UserManager.CheckRole(RoleSystem.Dictionary))
            {
                try
                {
                    var currentItem = eDataConttext.CompanyTypes.Single(e => e.ID == id);
                    UpdateModel(currentItem);
                    eDataConttext.SubmitChanges();
                    return RedirectToAction("Index");
                }
                catch (Exception exp)
                {
                    ViewData[Messages.MessageInfo] = exp.Message;
                    return View();
                }
            }
            else
            {
                ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                return View();
            }
        }
        #endregion

    }
}
